﻿Public Class frmPadrao
    Inherits System.Windows.Forms.Form

#Region " Enumerações "

    Enum eModo As Short
        eAguardar = 0
        eInserir = 1
        eEditar = 2
        eExcluir = 3
    End Enum

#End Region

#Region " Declarações de variáveis "

    Private mModo As eModo = eModo.eAguardar
    Private sMensagemConfirmacaoExclusao As String = "Deseja excluir o registro atual ?"
    Private iCodigo As Long
    Private bExclui As Boolean
    Private objPesquisa As New Controle.ctrPesquisa
#End Region

#Region " Eventos "

    Protected Shadows Event alteraModo(ByVal eModoAtual As eModo)

#End Region

#Region "Propriedades"

    Public Property Exclui() As Boolean
        Get
            Return Me.bExclui
        End Get
        Set(ByVal value As Boolean)
            Me.bExclui = value
        End Set
    End Property

    Public Property Codigo() As Long
        Get
            Return Me.iCodigo
        End Get
        Set(ByVal value As Long)
            Me.iCodigo = value
        End Set
    End Property

    Public Property modoAtual() As eModo
        Get
            Return mModo
        End Get
        Set(ByVal Value As eModo)
            mModo = Value
            alteraModopadrao(Value)
            RaiseEvent alteraModo(Value)
        End Set
    End Property

#End Region

#Region "Métodos privados"

    Private Sub cmdNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNovo.Click
        ClickNovo()
    End Sub

    Private Sub cmdExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExcluir.Click
        If MsgBox(sMensagemConfirmacaoExclusao, MsgBoxStyle.YesNo, Me.Text) = MsgBoxResult.Yes Then
            Me.Exclui = True
        Else
            Me.Exclui = False
        End If

    End Sub

    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        Me.ClickCancelar()
    End Sub

    Private Sub cmdSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSair.Click
        Me.Close()
    End Sub

    Private Sub alteraModopadrao(ByVal modo As eModo)

        Me.cmdNovo.Enabled = (modo = eModo.eExcluir Or modo = eModo.eAguardar)
        Me.cmdExcluir.Enabled = (modo = eModo.eExcluir Or modo = eModo.eAguardar And Me.Codigo > 0)
        Me.cmdGravar.Enabled = (modo = eModo.eInserir Or modo = eModo.eEditar)
        Me.cmdSair.Enabled = (modo = eModo.eExcluir Or modo = eModo.eAguardar)
        If (modo = eModo.eInserir Or modo = eModo.eEditar) Then
            cmdCancelar.Text = "Cancelar"
            cmdCancelar.Enabled = True
        ElseIf Me.Codigo > 0 Then
            cmdCancelar.Text = "Alterar"
            cmdCancelar.Enabled = True
        Else
            cmdCancelar.Enabled = False
        End If

    End Sub
    Private Sub frmPadrao_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.AbrirTela(eModo.eAguardar)

    End Sub
    Overridable Sub AbrirTela(ByVal modo As eModo)

        Me.Limpar(Me)
        Me.Codigo = 0
        Me.modoAtual = modo

    End Sub
    Overridable Sub ClickNovo()
        Me.Codigo = 0
        Me.Limpar(Me)
        Me.cmdCancelar.Text = "Cancelar"
        Me.modoAtual = eModo.eInserir
    End Sub
    Overridable Sub ClickCancelar()
        If (Me.modoAtual = eModo.eAguardar) Then
            Me.modoAtual = eModo.eEditar
        ElseIf (Me.modoAtual = eModo.eEditar Or Me.modoAtual = eModo.eInserir) Then
            If (Me.modoAtual = eModo.eInserir) Then
                Limpar(Me)
            End If
            Me.modoAtual = eModo.eAguardar
        End If
    End Sub
    Protected Sub Limpar(ByVal controlp As Control)
        Dim ctl As Control
        For Each ctl In controlp.Controls
            If TypeOf ctl Is TextBox Then
                DirectCast(ctl, TextBox).Text = String.Empty
            ElseIf TypeOf ctl Is RichTextBox Then
                DirectCast(ctl, RichTextBox).Text = String.Empty
            ElseIf TypeOf ctl Is CheckBox Then
                DirectCast(ctl, CheckBox).Checked = False
            ElseIf ctl.Controls.Count > 0 Then
                Limpar(ctl)
            End If
        Next
    End Sub

#End Region

#Region "Métodos Publicos"
    Public Sub PreencherCombos(ByVal cboCombo As Windows.Forms.ComboBox, ByVal sql As String, ByVal Campo As String, ByVal selecionado As Integer)
        Dim DS As New DataSet

        DS = Me.objPesquisa.Pesquisa(sql)

        cboCombo.DataSource = DS.Tables(0)
        cboCombo.DisplayMember = DS.Tables(0).Columns(Campo).ToString
        cboCombo.ValueMember = DS.Tables(0).Columns("IDEstado").ColumnName


    End Sub

    Public Function converteGridParaDataset(ByRef dtgridValor As DataGridView, _
                                            ByVal sNomeTabela As String) As DataSet

        Dim dsRetorno As New DataSet
        Dim datValor As New DataTable
        Dim byColuna As Byte = 0
        Dim bLinhaComDados As Boolean = False
        Dim aCampos(dtgridValor.ColumnCount - 1) As Object
        Dim dsOrigem As New DataSet

        'Verifica quais parametros foram passados
        If sNomeTabela <> String.Empty Then
            datValor.TableName = sNomeTabela
        End If

        'Monta as colunas do DataTable
        With dtgridValor
            For byColuna = 0 To .ColumnCount - 1
                If .Columns(byColuna).ValueType Is Nothing Then
                    datValor.Columns.Add(.Columns(byColuna).HeaderText, GetType(String))
                Else
                    datValor.Columns.Add(.Columns(byColuna).HeaderText, .Columns(byColuna).ValueType)
                End If
            Next
        End With

        dsRetorno.Tables.Add(datValor)
        'Adiciona as linhas do DataSet
        With dsRetorno.Tables(0)
            For shtLinha = 1 To dtgridValor.Rows.Count - 1
                bLinhaComDados = False
                For byColuna = 0 To dtgridValor.ColumnCount - 1
                    If dtgridValor.Item(byColuna, shtLinha).Value Is Nothing Then
                        aCampos(byColuna) = DBNull.Value
                    Else
                        bLinhaComDados = True
                        aCampos(byColuna) = dtgridValor.Item(byColuna, shtLinha).Value
                    End If
                Next
                If bLinhaComDados Then
                    .Rows.Add(aCampos)
                End If
            Next
        End With

        Return dsRetorno

    End Function

#End Region
End Class
